/**
 * Control.java [V 1.0.0]
 * classes : com.xiaocq.wordsgod.model.Control
 * xiaocq Create at 2012-6-30 下午6:50:54
 */
package com.xiaocq.wordsgod.model;

import java.util.Arrays;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

/**
 * com.xiaocq.wordsgod.model.Control
 * @author Mystory
 * Create at 2012-6-30 下午6:50:54
 */
public class Control {
	public static final String PREF_NAME = "testControl";
	
	public static final boolean CHOICE = true;
	public static final boolean COMPLETION = false;
	public static final String TEST_ID = "testId";
	
	private static final String TEST_TYPE = "testType";
	private static final String TEST_TIMESTAMP = "testTimestamp";
	private static final String TOTAL_NUM = "totalNum";
	private static final String TESTED_NUM = "testedNum";
	private static final String CORRECT_NUM = "correctNum";
	private static final String TOTAL_TEST_TIME = "totalTestTime";
	private static final String IS_NEW_TEST = "isNewTest";
	private static final String WORD_SCORE = "wordScore";
	
	private int testId;	//测试号(单元 or 自定类型)
	private boolean testType;	//测试类型 (选择 or 填空)
	private long testTimestamp; //此次测试的时间戳
	private int totalNum;	//总答题数目
	private int testedNum;	//已答题数目
	private int correctNum;	//已答对题数目
	private long totalTestTime; //累计测试时间
	private boolean isNewTest; //是否是新的测试
	
	private int[] wordScore;	//存储测试结果的数组
	
	private Control(int testId, boolean testType, int totalNum){
		/*赋值初始化*/
		this.testId = testId;
		this.testType = testType;
		this.totalNum = totalNum;
		
		/*默认初始化*/
		this.testTimestamp = System.currentTimeMillis();
		this.testedNum = 0;
		this.correctNum = 0;
		this.totalTestTime = 0L;
		this.isNewTest = true;
	}
	
	private Control(int testId, boolean testType, long testTimestamp, int totalNum, int testedNum, int correctNum, long totalTestTime, boolean isNewTest){
		this.testId = testId;
		this.testType = testType;
		this.testTimestamp = testTimestamp;
		this.totalNum = totalNum;
		this.testedNum = testedNum;
		this.correctNum = correctNum;
		this.totalTestTime = totalTestTime;
		this.isNewTest = isNewTest;
	}

	public int[] getWordScore() {
		return wordScore;
	}

	public void setWordScore(int[] wordScore) {
		this.wordScore = wordScore;
	}
	
	public int getTotalNum() {
		return totalNum;
	}

	public int getTestId() {
		return testId;
	}

	public boolean getTestType() {
		return testType;
	}

	public long getTestTimestamp() {
		return testTimestamp;
	}

	public int getTestedNum() {
		return testedNum;
	}

	public int getCorrectNum() {
		return correctNum;
	}

	public long getTotalTestTime() {
		return totalTestTime;
	}
	
	public boolean isNewTest() {
		return isNewTest;
	}
	
	public int getRestTestNum(){
		return totalNum - testedNum;
	}
	
	public int getWrongCount(){
		return totalNum - correctNum;
	}
	
	public void setNewTest(boolean isNewTest){
		this.isNewTest = isNewTest;
	}
	
	/**
	 * 当一道题结束时记录答题结果
	 * @param result
	 * @param timCost
	 */
	public void recordResult(boolean result, long timeCost){
		int answer;
		int state;
		
		if(result){
			correctNum += 1;
			answer = 1;
		}else{
			answer = 0;
		}
		
		state = wordScore[testedNum];
		wordScore[testedNum] = state * 10 + answer;
		totalTestTime += timeCost;
		testedNum += 1;
	}

	/**
	 * 判断是否是新的测试
	 * @param pref
	 * @return
	 */
	public static boolean isNewTest(SharedPreferences pref){
		boolean isNewTest = pref.getBoolean(IS_NEW_TEST, true);
		
		return isNewTest;
	}
	
	public void clearTest(SharedPreferences pref){
		Editor editor = pref.edit();
		editor.putBoolean(IS_NEW_TEST, true);
		editor.commit();
	}
	
	public void resetTest(){
		correctNum = 0;
		testedNum = 0;
		totalTestTime = 0L;
		
		for(int i=0; i<wordScore.length; i++){
			wordScore[i] = wordScore[i] / 10; 
		}
	}

	/**
	 * 新建一个Control类
	 * @param testId
	 * @param testType
	 * @param totalNum
	 * @return
	 */
	public static Control newInstance(int testId, boolean testType, int totalNum){
		return new Control(testId, testType, totalNum);
	}

	/**
	 * 从xml文件中读取控制信息
	 * @param pref
	 * @return
	 */
	public static Control loadFromXml(SharedPreferences pref){
		Control mControl;
		
		int mTestId = pref.getInt(TEST_ID, 1);
		boolean mTestType = pref.getBoolean(TEST_TYPE, false);
		long mTestTimestamp = pref.getLong(TEST_TIMESTAMP, 0L);
		int mTotalNum = pref.getInt(TOTAL_NUM, 0);
		int mTestedNum = pref.getInt(TESTED_NUM, 0);
		int mCorrectNum = pref.getInt(CORRECT_NUM, 0);
		long mTotalTestTime = pref.getLong(TOTAL_TEST_TIME, 0L);
		boolean mIsNewTest = false;
		String mWordScore = pref.getString(WORD_SCORE, "");
		
		mControl = new Control(mTestId, mTestType, mTestTimestamp, mTotalNum, mTestedNum, mCorrectNum, mTotalTestTime, mIsNewTest);
		mControl.setWordScore(convertStringToArray(mWordScore));
		
		return mControl;
	}
	
	/**
	 * 将控制信息保存到xml文件中
	 * @param pref
	 */
	public void saveToXml(SharedPreferences pref){
		Editor editor = pref.edit();
		
		editor.putInt(TEST_ID, testId);
		editor.putBoolean(TEST_TYPE, testType);
		editor.putLong(TEST_TIMESTAMP, testTimestamp);
		editor.putInt(TOTAL_NUM, totalNum);
		editor.putInt(TESTED_NUM, testedNum);
		editor.putInt(CORRECT_NUM, correctNum);
		editor.putLong(TOTAL_TEST_TIME, totalTestTime);
		editor.putString(WORD_SCORE, convertArrayToString(wordScore));
		editor.putBoolean(IS_NEW_TEST, false);
		
		editor.commit();
	}
	
	private static int[] convertStringToArray(String str){
		String[] scoreStr = str.split(",");
		int[] wordScore = new int[scoreStr.length];
		int index = 0;
		
		for(String element : scoreStr){
			wordScore[index++] = Integer.valueOf(element);
		}
		
		return wordScore;
	}
	
	private String convertArrayToString(int[] wordScore){
		StringBuilder builder = new StringBuilder();
		
		for(int score : wordScore){
			builder.append(score);
			builder.append(",");
		}
		
		return builder.toString();
	}
	
	public static Control convertToControl(Intent intent){
		Control mControl;
		int mTestId = intent.getIntExtra(TEST_ID, 1);
		boolean mTestType = intent.getBooleanExtra(TEST_TYPE, false);
		long mTestTimestamp = intent.getLongExtra(TEST_TIMESTAMP, 0L);
		int mTotalNum = intent.getIntExtra(TOTAL_NUM, 0);
		int mTestedNum = intent.getIntExtra(TESTED_NUM, 0);
		int mCorrectNum = intent.getIntExtra(CORRECT_NUM, 0);
		long mTotalTestTime = intent.getLongExtra(TOTAL_TEST_TIME, 0L);
		boolean mIsNewTest = intent.getBooleanExtra(IS_NEW_TEST, true);
		int[] mWordScore = intent.getIntArrayExtra(WORD_SCORE);
		
		mControl = new Control(mTestId, mTestType, mTestTimestamp, mTotalNum, mTestedNum, mCorrectNum, mTotalTestTime, mIsNewTest);
		mControl.setWordScore(mWordScore);
		
		return mControl;
	}
	
	public Intent convertToIntent(){
		Intent intent = new Intent();
		
		intent.putExtra(TEST_ID, testId);
		intent.putExtra(TEST_TYPE, testType);
		intent.putExtra(TEST_TIMESTAMP, testTimestamp);
		intent.putExtra(TOTAL_NUM, totalNum);
		intent.putExtra(TESTED_NUM, testedNum);
		intent.putExtra(CORRECT_NUM, correctNum);
		intent.putExtra(TOTAL_TEST_TIME, totalTestTime);
		intent.putExtra(WORD_SCORE, wordScore);
		intent.putExtra(IS_NEW_TEST, isNewTest);
		
		return intent;
	}

	@Override
	public String toString() {
		return "Control [testId=" + testId + ", testType=" + testType
				+ ", testTimestamp=" + testTimestamp + ", totalNum=" + totalNum
				+ ", testedNum=" + testedNum + ", correctNum=" + correctNum
				+ ", totalTestTime=" + totalTestTime + ", isNewTest="
				+ isNewTest + ", wordScore=" + Arrays.toString(wordScore) + "]";
	}
	
}
